package xcmg.device.service.company;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.company.CompanyDealerDO;
import xcmg.device.dao.entity.company.CompanyInfoDO;
import xcmg.device.dao.entity.company.CompanyInvitationCodeDO;
import xcmg.device.dao.entity.company.CompanySupplierDO;
import xcmg.device.dao.mapper.CompanyDealerMapper;
import xcmg.device.dao.mapper.CompanyInfoMapper;
import xcmg.device.dao.mapper.CompanyInvitationCodeMapper;
import xcmg.device.dao.mapper.CompanySupplierMapper;
import xcmg.device.dao.mapper.company.DealerVehicleMapper;
import xcmg.device.infra.CompanyConstants;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.ApplyStatusEnum;
import xcmg.device.infra.enums.CompanyDealerStatusEnum;
import xcmg.device.infra.exception.CommonBusiExceptions;
import xcmg.device.service.basic.CommonGenService;
import xcmg.device.service.vo.company.AuditApplyVO;
import xcmg.device.service.vo.company.CompanyInfoVO;
import xcmg.device.service.vo.company.CompanyRegisterVO;
import xcmg.device.service.vo.company.CompanySupplierVO;
import xcmg.device.util.CommonUtils;
import xcmg.device.util.CompanyUtil;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.infra.log.LogHelper;
import yb.ecp.fast.infra.util.PageHelperPlus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by sqq on 2017/12/22.
 */
@Service
public class CompanySupplierService
{
    @Autowired
    private CompanySupplierMapper companySupplierMapper;

    @Autowired
    private CompanyInvitationCodeMapper companyInvitationCodeMapper;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private CompanyApplyService companyApplyService;

    @Autowired
    private CompanyDealerMapper companyDealerMapper;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private CompanyUtil companyUtil;

    @Autowired
    private CommonGenService commonGenService;

    @Autowired
    private CompanyInfoMapper companyInfoMapper;


    @Value("${company.setting.inviteExpireDay}")
    private Integer inviteExpireDay;

    @Value("${company.setting.invitationCodeLength}")
    private Integer invitationCodeLength;
    @Autowired
    private DealerVehicleMapper dealerVehicleMapper;

    public ErrorCode insert(CompanySupplierVO companySupplierVO) {
        String textID = commonGenService.textIdWithException();
        companySupplierVO.setId(commonGenService.longIdWithException());
        companySupplierVO.setSupplierOrgId(textID);
        companySupplierVO.setCreateDate(new Date());
        companySupplierVO.setUpdateDate(new Date());
        companySupplierVO.setIsDel(false);
        companySupplierVO.setIsPlatCompany(false);
        boolean r=companySupplierMapper.insert(getDO(companySupplierVO)) > 0;
        boolean isYN = companyUtil.isYN(companySupplierVO.getOrgId());
        if (r && isYN) {
            //一步完成、邀请、注册、审核
            return doRegistAndAudit(companySupplierVO);
        }
        return r ? ErrorCode.Success : ErrorCode.Failure;
    }

    private ErrorCode doRegistAndAudit(CompanySupplierVO companySupplierVO)  {
        CompanySupplierVO companyDealerVO2 = new CompanySupplierVO();
        companyDealerVO2.setId(companySupplierVO.getId());
        ErrorCode code = invite(companyDealerVO2, companySupplierVO.getCreateId());
        if (code.getCode() == ErrorCode.Success.getCode()) {
            CompanyRegisterVO companyRegisterVO = new CompanyRegisterVO();
            companyRegisterVO.setCompanyName(companySupplierVO.getCompanyName());
            companyRegisterVO.setOrgCode(companySupplierVO.getSupplierOrgId());
            companyRegisterVO.setInvitationCode(companyDealerVO2.getInvitationCode());
            ErrorCode errorCode = companyInfoService.register(companyRegisterVO);
            if (errorCode.getCode() == ErrorCode.Success.getCode()) {
                AuditApplyVO auditApplyVO = new AuditApplyVO();
                auditApplyVO.setAuditName(companySupplierVO.getCreateName());
                auditApplyVO.setAuditId(companySupplierVO.getCreateId());
                auditApplyVO.setAuditStatus(ApplyStatusEnum.Pass.getCode());
                auditApplyVO.setAuditRemark("云内创建");
                auditApplyVO.setIsSap(0);
                auditApplyVO.setIsBarcode(0);
                auditApplyVO.setApplyId(companyRegisterVO.getApplyId() + "");
                auditApplyVO.setDealerType(3);
                return companyApplyService.audit(auditApplyVO);
            }else{
                throw new CommonBusiExceptions(errorCode.getDesc());
            }
        }
        throw new CommonBusiExceptions(code.getDesc());
    }

//    //同步供应商
//    @Async
//    public void getSap(String orgId){
//        CompanyInfoVO companyInfoVO=companyInfoService.info(orgId);
//        if(companyInfoVO==null || null == companyInfoVO.getSapCode()){
//            LogHelper.error("同步sap供应商时企业不存在",ErrorCode.RequestIdError.getCode());
//            return;
//        }
//        ImportParam importParam = new ImportParam();
//        importParam.setFunction("ZPMSBJ_GET_LIFNR");
//        importParam.setTable("LIFNROUT");
//        Map<String, String> map = new HashMap<>();
//        map.put("EKORG", companyInfoVO.getSapCode());
//        Date date=new Date();
//        DateFormat format=new SimpleDateFormat("yyyyMMdd");
//        map.put("I_PSTMP", format.format(date));
//        importParam.setCommonParamMap(map);
//        importParam.setSapCode(companyInfoVO.getSapCode());
//        LogHelper.monitor("========调用同步供应商接口开始========");
//        OutputParam outputParam = SAPCommonUtil.getData(importParam);
//        LogHelper.monitor("========调用同步供应商接口结束========");
//        if(outputParam==null){
//            LogHelper.error("sap返回值为null",ErrorCode.RequestIdError.getCode());
//            return;
//        }
//        for (Map<String,String> info:outputParam.getDataList()) {
//            CompanySupplierDO companySupplierDO=new CompanySupplierDO();
//            companySupplierDO.setOrgId(orgId);
//            companySupplierDO.setSupplierCode(info.get("LIFNR"));
//            companySupplierDO.setAddress(info.get("ADRNR"));
//            companySupplierDO.setCompanyName(info.get("NAME1"));
//            companySupplierDO.setContactTel(info.get("TELF1_LXR"));
//            companySupplierDO.setContact(info.get("NAME1_LXR"));
//            CompanySupplierDO supplierDO=companySupplierMapper.itemByCode(companySupplierDO.getOrgId(),companySupplierDO.getSupplierCode());
//            if(supplierDO==null){
//                ActionResult<String> textResult=fastGenClient.textGuid();
//                if(textResult.getCode()!=ErrorCode.Success.getCode()){
//                    LogHelper.monitor("同步sap供应商时获取orgId失败");
//                    continue;
//                }
//                companySupplierDO.setSupplierOrgId(textResult.getValue());
//                companySupplierDO.setCreateDate(new Date());
//                companySupplierDO.setUpdateDate(new Date());
//                companySupplierDO.setIsDel(false);
//                companySupplierDO.setIsPlatCompany(false);
//                companySupplierMapper.insert(companySupplierDO);
//            }
//            else{
//                companySupplierDO.setUpdateDate(new Date());
//                companySupplierMapper.update(companySupplierDO);
//            }
//        }
//    }

    public ErrorCode update(CompanySupplierVO companySupplierVO)
    {
        CompanySupplierVO newVO = new CompanySupplierVO();
        newVO.setUpdateDate(new Date());
        newVO.setId(companySupplierVO.getId());
        newVO.setSupplierCode(companySupplierVO.getSupplierCode());
        newVO.setCompanyName(companySupplierVO.getCompanyName());
        newVO.setShortName(companySupplierVO.getShortName());
        newVO.setAddress(companySupplierVO.getAddress());
        newVO.setZip(companySupplierVO.getZip());
        newVO.setFax(companySupplierVO.getFax());
        newVO.setContact(companySupplierVO.getContact());
        newVO.setContactTel(companySupplierVO.getContactTel());
        newVO.setSupplierClassId(companySupplierVO.getSupplierClassId());
        newVO.setUpdateId(companySupplierVO.getUpdateId());
        newVO.setOwerId(companySupplierVO.getOwerId());
        newVO.setIsDirect(companySupplierVO.getIsDirect());
        newVO.setIsDirectHF(companySupplierVO.getIsDirectHF());
        newVO.setDeptCode(companySupplierVO.getDeptCode());
        newVO.setDeptName(companySupplierVO.getDeptName());
        newVO.setPurchaseCode(companySupplierVO.getPurchaseCode());
        newVO.setPurchaseName(companySupplierVO.getPurchaseName());
        newVO.setKunmingSettlement(companySupplierVO.getKunmingSettlement());
        return companySupplierMapper.update(getDO(newVO)) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }


    /**
     * 通过验证码绑定代理商信息
     * @param orgId String
     * @param invitationCode String
     * @return ErrorCode
     */
    public ErrorCode boundSupplier(String orgId, String invitationCode) {
        CompanyInvitationCodeDO companyInvitationCodeDO = new CompanyInvitationCodeDO();
        companyInvitationCodeDO.setInvitationCode(invitationCode);
        companyInvitationCodeDO = companyInfoService.queryInviteInfo(invitationCode);
        if (null == companyInvitationCodeDO) {
            return ErrorCode.InvitationCodeFail;
        }
        if (orgId.equals(companyInvitationCodeDO.getSenderOrgId())) {
            return ErrorCode.CannotBoundSelf;
        }

        if (!companyInvitationCodeDO.getInviteType().equals(1)) {
            return ErrorCode.InvitationCodeFail;
        }
        //邀请码发送方企业信息
        CompanyInfoVO companyInfoVO = companyInfoService.info(companyInvitationCodeDO.getSenderOrgId());
        if (null == companyInfoVO) {
            return ErrorCode.RelationSupplierCompanyFail;
        }
        int supplierCount = companySupplierMapper.countSupplierByInvitationCode(orgId, companyInvitationCodeDO.getSenderOrgId());
        if (supplierCount > 0) {
            return ErrorCode.SupplierCompanyRepeat;
        }
        CompanySupplierDO companySupplierDO = companyApplyService.getSupplierDO(companyInfoVO, orgId);
        int ret = companySupplierMapper.insert(companySupplierDO);
        if (0 >= ret) {
            return ErrorCode.RelationSupplierCompanyFail;
        }
        //更改客户关系
        //修改企业上下游关系表中下游企业状态
        companyDealerMapper.updateApplyStatus(companyInvitationCodeDO.getSenderOrgId(), companyInvitationCodeDO.getReceiverOrgId(), CompanyConstants.APPLY_STATUS_PASS);
        //更新企业客户关系中绑定关系
        companyDealerMapper.boundDealer(orgId, companyInvitationCodeDO.getSenderOrgId(), companyInvitationCodeDO.getReceiverOrgId());
        companyInvitationCodeDO.setCodeState(1);//邀请码状态为无效
        companyInvitationCodeMapper.update(companyInvitationCodeDO);
        return ErrorCode.Success;
    }


    @Transactional
    public ErrorCode delete(List<Long> ids, String userId)
    {
        ErrorCode ret = ErrorCode.Success;
        for (Long id : ids)
        {
            CompanySupplierDO companySupplierDO =companySupplierMapper.item(id);
            CompanySupplierVO newVO = new CompanySupplierVO();
            newVO.setUpdateDate(new Date());
            newVO.setIsDel(true);
            newVO.setId(id);
            newVO.setUpdateId(userId);
            ret = companySupplierMapper.update(getDO(newVO)) > 0 ? ErrorCode.Success : ErrorCode.Failure;
            //修改下游表对应信息
            CompanyDealerDO CompanyDealerDO=companyDealerMapper.itemByOrgId(companySupplierDO.getSupplierOrgId(),companySupplierDO.getOrgId());
            if(CompanyDealerDO!=null){
                CompanyDealerDO.setIsDel(true);
                ret =companyDealerMapper.update(CompanyDealerDO)> 0 ? ErrorCode.Success : ErrorCode.Failure;
                if (ErrorCode.Success == ret){
                    dealerVehicleMapper.deleteByClientId(String.valueOf(CompanyDealerDO.getId()));
                }
            }
        }
        return ret;
    }

    public PageCommonVO<Object> list(SearchCommonVO<CompanySupplierVO> condition) {
        PageCommonVO<Object> pageCommonVO = new PageCommonVO<>();
        List<CompanySupplierDO> doList = null;
        if(condition.getFilters() != null) {
            CompanySupplierVO companySupplierVO = condition.getFilters();
            boolean direct = Integer.valueOf(1).equals(companySupplierVO.getIsDirect()) || Integer.valueOf(1).equals(companySupplierVO.getIsDirectHF());
            if (direct && !StringUtils.isEmpty(companySupplierVO.getOrgId())) {
                CompanyInfoVO companyInfoVO = companyInfoService.info(companySupplierVO.getOrgId());
                if (!String.valueOf(1).equals(companyInfoVO.getCompanyType())) {
                    CompanyInfoVO filters = new CompanyInfoVO();
                    filters.setCompanyName(companySupplierVO.getCompanyName());
                    filters.setCompanyType(String.valueOf(1));
                    filters.setOrgCode(companySupplierVO.getSupplierCode());
                    PageHelperPlus.orderBy("create_date desc");
                    PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());

                    List<CompanyInfoDO> companyInfoDOs = companyInfoMapper.list(filters);
                    List<Object> voList = new ArrayList<>();
                    for (CompanyInfoDO companyInfoDO : companyInfoDOs) {
                        CompanySupplierVO supplierDO = new CompanySupplierVO();
                        supplierDO.setOrgId(companyInfoVO.getOrgId());
                        supplierDO.setSupplierOrgId(companyInfoDO.getOrgId());
                        supplierDO.setCompanyName(companyInfoDO.getCompanyName());
                        supplierDO.setSupplierCode(companyInfoDO.getOrgCode());
                        voList.add(supplierDO);
                    }
                    pageCommonVO.setPageInfo(new PageInfo<>(companyInfoDOs));
                    pageCommonVO.setPageInfoList(voList);
                    return pageCommonVO;
                }
            }
        }

        List<Object> voList = new ArrayList<>();
        doList(pageCommonVO,doList,voList,condition);
        return pageCommonVO;
    }

    private void doList(PageCommonVO<Object> pageCommonVO, List<CompanySupplierDO> doList, List<Object> voList,SearchCommonVO<CompanySupplierVO> condition) {
        PageHelperPlus.orderBy("ID ");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        if (condition.getFilters().getIsPlatCompany() != null && condition.getFilters().getIsPlatCompany()) {
            doList = companySupplierMapper.listIsPlatCompany(condition.getFilters());
        } else {
            doList = companySupplierMapper.list(condition.getFilters());
        }

        for (CompanySupplierDO companySupplierDO : doList) {
            if (companySupplierDO == null) {
                continue;
            }
            voList.add(getVO(companySupplierDO));
        }
        pageCommonVO.setPageInfo(new PageInfo<>(doList));
        pageCommonVO.setPageInfoList(voList);
    }

    public ErrorCode invite(CompanySupplierVO companySupplierVO, String userId) {
        CompanySupplierDO companySupplierDO=companySupplierMapper.item(companySupplierVO.getId());
        if(companySupplierDO==null){
            return ErrorCode.IllegalArument;
        }
        String oldInvitationCode=checkInvite(companySupplierDO.getOrgId(),companySupplierDO.getSupplierOrgId());
        if(oldInvitationCode!=null){
            companySupplierVO.setInvitationCode(oldInvitationCode);
            return ErrorCode.Success;
        }
        String invitationCode = CommonUtils.generateString(invitationCodeLength);
        companySupplierVO.setInvitationCode(invitationCode);
        CompanyInvitationCodeDO companyInvitationCodeDO=new CompanyInvitationCodeDO();
        companyInvitationCodeDO.setSenderOrgId(companySupplierDO.getOrgId());
        companyInvitationCodeDO.setReceiverOrgId(companySupplierDO.getSupplierOrgId());
        companyInvitationCodeDO.setDestPhone(companySupplierDO.getContactTel());
        companyInvitationCodeDO.setInviteType(2);
        companyInvitationCodeDO.setInvitationCode(invitationCode);
        companyInvitationCodeDO.setInviteUserId(userId);
        companyInvitationCodeDO.setCodeState(0);
        companyInvitationCodeDO.setInviteDate(new Date());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, +inviteExpireDay);
        companyInvitationCodeDO.setExpireDate(cal.getTime());

        int insertRet = companyInvitationCodeMapper.insert(companyInvitationCodeDO);

        if (0 >= insertRet)
        {
            LogHelper.error("企业邀请码添加失败！", ErrorCode.SqlSyntaxError.getCode());
            return ErrorCode.SqlSyntaxError;
        }

        int updateRet = companySupplierMapper.updateApplyStatus(companyInvitationCodeDO.getSenderOrgId(), companyInvitationCodeDO.getReceiverOrgId(), CompanyConstants.APPLY_STATUS_INVITED);
        if (0 >= updateRet)
        {
            LogHelper.error("修改企业客户关系中审核状态为已邀请操作失败，数据库异常！", ErrorCode.SqlSyntaxError.getCode());
            return ErrorCode.SqlSyntaxError;
        }
        return ErrorCode.Success;
    }

    private String checkInvite(String orgId,String dealerOrgId){
        CompanyInvitationCodeDO companyInvitationCodeDO=new CompanyInvitationCodeDO();
        companyInvitationCodeDO.setSenderOrgId(orgId);
        companyInvitationCodeDO.setReceiverOrgId(dealerOrgId);
        companyInvitationCodeDO.setInviteDate(new Date());
        companyInvitationCodeDO.setExpireDate(new Date());
        companyInvitationCodeDO.setCodeState(0);
        CompanyInvitationCodeDO inviteInfo=companyInvitationCodeMapper.invitationInfo(companyInvitationCodeDO);
        if(inviteInfo==null){
            return null;
        }

        if(0!=inviteInfo.getCodeState())
        {
            return null;
        }

        return inviteInfo.getInvitationCode();
    }



    private CompanySupplierDO getDO(CompanySupplierVO companySupplierVO)
    {
        if (companySupplierVO == null)
        {
            return null;
        }
        CompanySupplierDO companySupplierDO = new CompanySupplierDO();
        companySupplierDO.setId(companySupplierVO.getId());
        companySupplierDO.setOrgId(companySupplierVO.getOrgId());
        companySupplierDO.setSupplierOrgId(companySupplierVO.getSupplierOrgId());
        companySupplierDO.setSupplierCode(companySupplierVO.getSupplierCode());
        companySupplierDO.setCompanyName(companySupplierVO.getCompanyName());
        companySupplierDO.setShortName(companySupplierVO.getShortName());
        companySupplierDO.setAddress(companySupplierVO.getAddress());
        companySupplierDO.setZip(companySupplierVO.getZip());
        companySupplierDO.setFax(companySupplierVO.getFax());
        companySupplierDO.setContact(companySupplierVO.getContact());
        companySupplierDO.setContactTel(companySupplierVO.getContactTel());
        companySupplierDO.setSupplierClassId(companySupplierVO.getSupplierClassId());
        companySupplierDO.setIsPlatCompany(companySupplierVO.getIsPlatCompany());
        companySupplierDO.setPlatOrgId(companySupplierVO.getPlatOrgId());
        companySupplierDO.setCreateDate(companySupplierVO.getCreateDate());
        companySupplierDO.setCreateId(companySupplierVO.getCreateId());
        companySupplierDO.setUpdateDate(companySupplierVO.getUpdateDate());
        companySupplierDO.setUpdateId(companySupplierVO.getUpdateId());
        companySupplierDO.setIsDel(companySupplierVO.getIsDel());
        companySupplierDO.setOwerId(companySupplierVO.getOwerId());
        companySupplierDO.setIsDirect(companySupplierVO.getIsDirect());
        companySupplierDO.setIsDirectHF(companySupplierVO.getIsDirectHF());
        companySupplierDO.setDeptCode(companySupplierVO.getDeptCode());
        companySupplierDO.setDeptName(companySupplierVO.getDeptName());
        companySupplierDO.setPurchaseCode(companySupplierVO.getPurchaseCode());
        companySupplierDO.setPurchaseName(companySupplierVO.getPurchaseName());
        companySupplierDO.setKunmingSettlement(companySupplierVO.getKunmingSettlement());
        return companySupplierDO;
    }

    private CompanySupplierVO getVO(CompanySupplierDO m)
    {
        if (m == null)
        {
            return null;
        }
        CompanySupplierVO supplierVO = new CompanySupplierVO();
        supplierVO.setId(m.getId());
        supplierVO.setOrgId(m.getOrgId());
        supplierVO.setSupplierOrgId(m.getSupplierOrgId());
        supplierVO.setSupplierCode(m.getSupplierCode());
        supplierVO.setCompanyName(m.getCompanyName());
        supplierVO.setShortName(m.getShortName());
        supplierVO.setAddress(m.getAddress());
        supplierVO.setZip(m.getZip());
        supplierVO.setFax(m.getFax());
        supplierVO.setContact(m.getContact());
        supplierVO.setContactTel(m.getContactTel());
        supplierVO.setSupplierClassId(m.getSupplierClassId());
        supplierVO.setIsPlatCompany(m.getIsPlatCompany());
        supplierVO.setPlatOrgId(m.getPlatOrgId());
        supplierVO.setCreateDate(m.getCreateDate());
        supplierVO.setCreateId(m.getCreateId());
        supplierVO.setUpdateDate(m.getUpdateDate());
        supplierVO.setUpdateId(m.getUpdateId());
        supplierVO.setIsDel(m.getIsDel());
        supplierVO.setOwerId(m.getOwerId());
        supplierVO.setIsDirect(m.getIsDirect());
        supplierVO.setIsDirectHF(m.getIsDirectHF());
        supplierVO.setSupplierClassName(m.getSupplierClassName());
        supplierVO.setApplyStatus(m.getApplyStatus());
        supplierVO.setIsSap(m.getIsSap());
        supplierVO.setDeptCode(m.getDeptCode());
        supplierVO.setDeptName(m.getDeptName());
        supplierVO.setPurchaseCode(m.getPurchaseCode());
        supplierVO.setPurchaseName(m.getPurchaseName());
        CompanyDealerStatusEnum dealerStatus = CompanyDealerStatusEnum.getDealerStatus(supplierVO.getApplyStatus());
        if(null!=dealerStatus)
        {
            supplierVO.setApplyStatusDesc(dealerStatus.getDesc());
        }
        supplierVO.setKunmingSettlement(m.getKunmingSettlement());
        return supplierVO;
    }

    public PageCommonVO<Object> listForPurchasePrice(SearchCommonVO<CompanySupplierVO> condition) {

        PageCommonVO<Object> pageCommonVO = new PageCommonVO<>();
        PageHelperPlus.orderBy("ID ");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        //update by huhuawei 2018-9-21  如果是平台企业 调用平台企业的查询方法
        List<CompanySupplierDO> doList = null;
        if (condition.getFilters().getIsPlatCompany() != null && condition.getFilters().getIsPlatCompany()) {
            doList = companySupplierMapper.listIsPlatCompany(condition.getFilters());
        } else {
            doList = companySupplierMapper.listForPurchasePrice(condition.getFilters());
        }
        List<Object> voList = new ArrayList<>();
        for (CompanySupplierDO companySupplierDO : doList) {
            if (companySupplierDO == null)
            {
                continue;
            }
            voList.add(getVO(companySupplierDO));
        }
        pageCommonVO.setPageInfo(new PageInfo<>(doList));
        pageCommonVO.setPageInfoList(voList);
        return pageCommonVO;
    }

    public PageCommonVO<Object> listSellUnit(SearchCommonVO<CompanySupplierVO> condition) {
        String orgId = condition.getFilters().getOrgId();
        PageHelperPlus.orderBy("company_type, org_id desc ");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<CompanySupplierDO> dos = companySupplierMapper.listSellUnit(condition.getFilters());
        PageCommonVO<Object> pageCommonVO = new PageCommonVO<>();
        pageCommonVO.setPageInfo(new PageInfo<CompanySupplierDO>(dos));
        List<Object> vos = new ArrayList<>();
        for (CompanySupplierDO companySupplierDO : dos) {
            if (companySupplierDO == null) {
                continue;
            }
            CompanySupplierVO vo = new CompanySupplierVO();
            BeanUtils.copyProperties(companySupplierDO, vo);
            vo.setOrgId(orgId);
            vos.add(vo);
        }
        pageCommonVO.setPageInfoList(vos);
        return pageCommonVO;
    }

    public boolean isYn(String orgId) {
        return companyUtil.isYN(orgId);
    }
}
